package com.cs.framework.jdbc.mybatis3.session;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 回话代理类
 * 
 * @author hedongzhou
 * 
 */
public class SelectSessionInvocation implements InvocationHandler {

	/**
	 * 工厂
	 */
	private SelectSessionFactory sessionFactory;

	public SelectSessionInvocation(SelectSessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * 代理
	 */
	public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
		// 开启回话
		SelectSession session = sessionFactory.openSession();

		Object result = null;
		try {
			// 调用方法
			result = method.invoke(session, params);
		} finally {
			// 结束回话
			session.close();
		}

		return result;
	}

}
